﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class ApplyUserList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);
            }
        }

        #region 加载内容
        private void LoadContent(bool mark)
        {
            //设置控件属性，从BasePage中得到
            PageConfig(UPager);

            //List<Member> list = new List<Member();
            UserCompanyHelper uchelper = new UserCompanyHelper();
            Criteria c = new Criteria();

            Users uinfo = UserBase.GetCurrentUser();
            c.Add(CriteriaType.Equals, "CompanyOwnerID", uinfo.ID);
            c.Add(CriteriaType.Equals, "Status", 0);


            UPager.ItemCount = uchelper.QueryUserCompanyCountBySearch(c);

            // 以下的值从BasePage中得到
            UPager.UrlFormat = UrlFormat;
            UPager.PrefixText = GetPreFixText(UPager);

            if (mark)
            {
                PagerInit(UPager);
            }

            string[] fields = new string[] { "ID", "UsersID", "NickName", "CompanyID","Remark", "CompanyOwnerID", "Status", "CreateDate" };

            DataTable dt = new DataTable();
            dt = uchelper.QueryUserCompanyBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, UPager.Begin - 1, UPager.Count, fields);

            dt.Columns.Add(new DataColumn("CompanyName"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string companyid = dr["CompanyID"] + "";
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(companyid);
                    string companyname = cinfo.CompanyName;
                    dr["CompanyName"] = companyname;
                }
            }

            rptApplyUserList.DataSource = dt;
            rptApplyUserList.DataBind();
        }
        #endregion

        #region 列表事件
        protected void rptApplyUserList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id = hidID.Value;

            if (e.CommandName == "ok")//同意
            {
                UserCompany ucinfo = new UserCompany();
                UserCompanyHelper uchelper = new UserCompanyHelper();
                ucinfo = uchelper.GetUserCompany(id);
                ucinfo.Status = 1;
                uchelper.EditUserCompany(ucinfo);

                //给申请人发送信息通知
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                Users uinfo = UserBase.GetCurrentUser();
                minfo.FromUsersID = uinfo.ID;
                minfo.FromNickName = uinfo.NickName;
                minfo.ToUsersID = ucinfo.UsersID;

                string companyid = ucinfo.CompanyID;
                Companys cinfo = new Companys();
                CompanysHelper chelper = new CompanysHelper();
                cinfo = chelper.GetCompanys(companyid);
                string companyname = cinfo.CompanyName;

                minfo.MessageContent = "加入小组 " + companyname + "审批通过，让我们一起努力吧";
                minfo.Status = 0;
                mhelper.AddMessage(minfo);
            }
            else if (e.CommandName == "no")//拒绝
            {
                UserCompany ucinfo = new UserCompany();
                UserCompanyHelper uchelper = new UserCompanyHelper();
                ucinfo = uchelper.GetUserCompany(id);
                ucinfo.Status = 2;
                uchelper.EditUserCompany(ucinfo);

                //给申请人发送信息通知
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                Users uinfo = UserBase.GetCurrentUser();
                minfo.FromUsersID = uinfo.ID;
                minfo.FromNickName = uinfo.NickName;
                minfo.ToUsersID = ucinfo.UsersID;
                minfo.MessageContent = "加入小组申请被拒绝，有问题请给我留言";
                minfo.Status = 0;
                mhelper.AddMessage(minfo);
            }

            Tools.Alert("操作成功");
            LoadContent(false);
        }
        #endregion
    }
}